package com.das.mechanic_base.widget;

import android.app.Activity;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.core.content.res.ResourcesCompat;

import com.das.mechanic_base.R;
import com.das.mechanic_base.utils.X3StringUtils;
import com.zyyoona7.wheel.WheelView;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import cn.carbswang.android.numberpickerview.library.NumberPickerView;

/**
 * Created by chensi on 2018/3/20.
 */

public class X3NumberPickerPopWindow implements PopupWindow.OnDismissListener, View.OnClickListener {

    private Activity mContext;
    private int pageNum;// 列数
    private PopupWindow popupWindow;
    private int childCount;
    private String[] obtainArr = null;
    private View view;

    private String[] numArr;
    private String[] numPoint = {"0","1","2","3","4","5","6","7","8","9"};

    private LinearLayout al_child;
    private TextView tv_num_picker;
    private TextView tv_slider;
    private final int j;

    private List<String> startNum;

    public X3NumberPickerPopWindow(Activity mContext, int pageNum , int maxNum, String startData) {
        this.mContext = mContext;
        this.pageNum = pageNum;
        obtainArr = new String[pageNum];
        for (int i = 0;i < pageNum ;i ++) {
            obtainArr[i] = 0+"";
        }
        for (int i = 0; i <= maxNum; i++) {
            if (i == 0) {
                numArr = new String[maxNum+1];
            }
            numArr[i] = i+"";
        }


        //设置PopupWindow的View
        view = LayoutInflater.from(mContext).inflate(R.layout.x3_number_picker, new LinearLayout(mContext),false);
        al_child = view.findViewById(R.id.al_child);
        tv_num_picker = view.findViewById(R.id.tv_num_picker);
        tv_slider = view.findViewById(R.id.tv_slider);

        j = al_child.getChildCount() - pageNum;
        if (j > 0) {
            for (int i = 0; i < j; i++) {
                al_child.getChildAt(i).setVisibility(View.GONE);
                al_child.setGravity(Gravity.CENTER);
            }
        }

        startNum = new ArrayList<>();
        if (!X3StringUtils.isEmpty(startData)) {
            if (startData.contains(".") && pageNum == 3) {
                String[] split = startData.split(".");
                String one = split[1].substring(0, 1);
                String two = split[1].substring(1,2);
                startNum.add(split[0]);
                startNum.add(one);
                startNum.add(two);
            }else if (startData.contains(".") && pageNum == 2) {
                String[] split = startData.split("\\.");
                startNum.add(split[0]);
                startNum.add(split[1]);
            }else {
                int num = pageNum - startData.length();
                if (num > 0) {
                    // 说明这个值前面的0 没有  需要补上
                    String zeroStr = "";
                    for (int i = 0; i < num; i++) {
                        zeroStr = zeroStr + "0";
                    }
                    startData = zeroStr + startData;
                }
                for (int i = 0; i < pageNum; i++) {
                    String content = startData.substring(i, i + 1);
                    startNum.add(content);
                }
            }
        }

        for (int i = j; i < al_child.getChildCount(); i++) {
            WheelView<String> wv_view = (WheelView<String>) al_child.getChildAt(i);
            wv_view.setData(Arrays.asList(numPoint));
            wv_view.setTypeface(ResourcesCompat.getFont(mContext,R.font.din_text_type));
            wv_view.setSelectedItemPosition(Integer.parseInt(startNum.get(i - j)));
            wv_view.setSoundEffectResource(R.raw.x3_button_choose);
            wv_view.setSoundEffect(true);
            wv_view.setPlayVolume(0.5f);
        }
    }

    /**
     * 显示小数点
     * @param isShow
     */
    public void showSliderTv(boolean isShow) {
        if (isShow) {
            tv_slider.setVisibility(View.VISIBLE);
        }else {
            tv_slider.setVisibility(View.GONE);
        }
    }

    public void setTitle(String title) {
        if (title != null && !"".equals(title)) {
            tv_num_picker.setText(title+"");
        }
    }

    private void setData (NumberPickerView numberPickerView,int mixValue,int maxValue,int value) {
        numberPickerView.refreshByNewDisplayedValues(numArr);
    }

    public void showPopupWindow (View v,int flag) {
        //防止重复按按钮
        if (popupWindow != null && popupWindow.isShowing()) {
            return;
        }

        int k = 0;
        childCount = al_child.getChildCount();
        for (int i = j; i < childCount; i ++ ) {
            View childAt = al_child.getChildAt(i);
            if (childAt instanceof NumberPickerView) {
                if (flag == 1 && k == 0) {
                    ((NumberPickerView)childAt).refreshByNewDisplayedValues(numArr);
                }else {
                    ((NumberPickerView)childAt).refreshByNewDisplayedValues(numPoint);
                }
                k++;
            }
        }
        popupWindow = new PopupWindow(view, RelativeLayout.LayoutParams.MATCH_PARENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);
        //设置背景,这个没什么效果，不添加会报错
        popupWindow.setBackgroundDrawable(null);
        //设置点击弹窗外隐藏自身
        popupWindow.setFocusable(true);
        popupWindow.setOutsideTouchable(true);
        //设置动画
        popupWindow.setAnimationStyle(R.style.PopupWindow1);
        //设置位置
        popupWindow.showAtLocation(v, Gravity.BOTTOM, 0, 0);
        //设置消失监听
        popupWindow.setOnDismissListener(this);
        //setBackgroundAlpha(0.9f);
        //设置PopupWindow的View点击事件
        setOnPopupViewClick(view);

    }

    private void setOnPopupViewClick(View view) {
        Button btn_cancel = view.findViewById(R.id.btn_cancel);
        Button btn_affirm = view.findViewById(R.id.btn_affirm);

        btn_affirm.setOnClickListener(this);
        btn_cancel.setOnClickListener(this);
    }

    //设置屏幕背景透明效果
    public void setBackgroundAlpha(float alpha) {
        WindowManager.LayoutParams lp = mContext.getWindow().getAttributes();
        lp.alpha = alpha;
        mContext.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);//此行代码主要是解决在华为手机上半透明效果无效的bug
        mContext.getWindow().setAttributes(lp);
    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.btn_affirm) {
            if (popupWindow != null) {
                popupWindow.dismiss();
            }
            if (iOnItemCameraClick != null) {
                iOnItemCameraClick.iOpenAffirmListener();
            }
        } else if (id == R.id.btn_cancel) {
            if (popupWindow != null) {
                popupWindow.dismiss();
            }
            if (iOnItemCameraClick != null) {
                iOnItemCameraClick.iOpenCancelListener();
            }
        }
    }

    IOpenNumberClick iOnItemCameraClick;

    public void setiOnItemCameraClick(IOpenNumberClick iOnItemCameraClick) {
        this.iOnItemCameraClick = iOnItemCameraClick;
    }

    @Override
    public void onDismiss() {
        if (iOnItemCameraClick != null) {
            iOnItemCameraClick.iOnShowView(false);
        }
    }

    public interface IOpenNumberClick {

        void iOpenCancelListener();

        void iOpenAffirmListener();

        void iOnShowView(boolean isShow);
    }

    public String getNumArr () {
        String selectContent = "";
        for (int i = j; i < childCount; i++) {
            View childAt = al_child.getChildAt(i);
            if (childAt instanceof WheelView) {
                WheelView wheelView = (WheelView) childAt;
                String value = (String) wheelView.getSelectedItemData();
                if (tv_slider.getVisibility() ==  View.VISIBLE && i == j + 1) {
                    selectContent = selectContent +"."+ value;
                }else {
                    selectContent = selectContent + value;
                }

            }
        }
        return selectContent;
    }
}
